package com.ruoyi.web.controller.durian;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.durian.domain.*;
import com.ruoyi.durian.domain.vo.CouponGrantVo;
import com.ruoyi.durian.service.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

/**
 * 管理员控制器
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/admin/manage")
public class AdminController extends BaseController {

    @Resource
    private LlThaliService llThaliService;

    @Resource
    private LlCouponService llCouponService;

    @Resource
    private LlMapService llMapService;

    @Resource
    private LlCarService llCarService;

    @Resource
    LlCouponExchangeService llCouponExchangeService;

    /**
     * 券码发布历史列表查询
     */
    @GetMapping("/couponExchange/list")
    public TableDataInfo list(LlCouponExchange llCouponExchange) {
        startPage();
        List<LlCouponExchange> couponExchangeList = llCouponExchangeService.selelctLlCouponExchange(llCouponExchange);
        return getDataTable(couponExchangeList);
    }

    /**
     * 查询地图模块-地图列表
     */
    @GetMapping("/map/list")
    public TableDataInfo list(LlMap llMap) {
        startPage();
        List<LlMap> list = llMapService.selectLlMapList(llMap);
        return getDataTable(list);
    }

    /**
     * 导出地图模块-地图列表
     */
    @PostMapping("/map/export")
    public void export(HttpServletResponse response, LlMap llMap) {
        List<LlMap> list = llMapService.selectLlMapList(llMap);
        ExcelUtil<LlMap> util = new ExcelUtil<LlMap>(LlMap.class);
        util.exportExcel(response, list, "地图数据");
    }

    /**
     * 获取地图模块-地图详细信息
     */
    @GetMapping(value = "/map/query/{id}")
    public AjaxResult mapGetInfo(@PathVariable("id") Long id) {
        return success(llMapService.selectLlMapById(id));
    }

    /**
     * 新增地图模块-地图
     */
    @PostMapping("/map/add")
    public AjaxResult add(@RequestBody LlMap llMap) {
        return AjaxResult.success(llMapService.insertLlMap(llMap));
    }

    /**
     * 修改地图模块-地图
     */
    @PostMapping("/map/edit")
    public AjaxResult edit(@RequestBody LlMap llMap) {
        return AjaxResult.success(llMapService.updateLlMap(llMap));
    }

    /**
     * 删除地图模块-地图
     */
    @DeleteMapping("/map/remove/{ids}")
    public AjaxResult mapRemove(@PathVariable Long[] ids) {
        return AjaxResult.success(llMapService.deleteLlMapByIds(ids));
    }

    /**
     * 查询优惠券模块-优惠券列表
     */
    @GetMapping("/couponList")
    public TableDataInfo list(LlCoupon llCoupon) {
        startPage();
        List<LlCoupon> list = llCouponService.selectLlCouponList(llCoupon);
        return getDataTable(list);
    }

    /**
     * 导入地图
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/map/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<LlMap> util = new ExcelUtil<LlMap>(LlMap.class);
        List<LlMap> mapList = util.importExcel(file.getInputStream());
        String message = llMapService.importUser(mapList, updateSupport);
        return success(message);
    }

    /**
     * 地图导入
     *
     * @param response
     */
    @PostMapping("/map/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<LlMap> util = new ExcelUtil<LlMap>(LlMap.class);
        util.importTemplateExcel(response, "用户数据");
    }

    /**
     * 导出优惠券模块-优惠券列表
     */
    @PostMapping("coupon/export")
    public void export(HttpServletResponse response, LlCoupon llCoupon) {
        List<LlCoupon> list = llCouponService.selectLlCouponList(llCoupon);
        ExcelUtil<LlCoupon> util = new ExcelUtil<LlCoupon>(LlCoupon.class);
        util.exportExcel(response, list, "优惠券数据");
    }

    /**
     * 获取优惠券模块-优惠券详情
     */
    @GetMapping(value = "/coupon/getInfo/{id}")
    public AjaxResult getInfoCoupon(@PathVariable("id") Long id) {
        return success(llCouponService.selectLlCouponById(id));
    }

    /**
     * 新增优惠券模块-优惠券
     */
    @PostMapping("/grant/addCoupon")
    public AjaxResult add(@RequestBody LlCoupon llCoupon) {
        if (llCoupon == null) {
            return AjaxResult.error("无效参数");
        }
        return AjaxResult.success(llCouponService.insertLlCoupon(llCoupon));
    }

    /**
     * 修改优惠券
     *
     * @param llCoupon
     * @return
     */
    @PostMapping("/grant/updateCoupon")
    public AjaxResult edit(@RequestBody LlCoupon llCoupon) {
        return AjaxResult.success(llCouponService.updateLlCoupon(llCoupon));
    }

    /**
     * 删除优惠券模块-优惠券
     */
    @DeleteMapping("/coupon/delCoupon/{ids}")
    public AjaxResult couponRemove(@PathVariable Long[] ids) {
        return AjaxResult.success(llCouponService.deleteLlCouponByIds(ids));
    }

    /**
     * 获取失效券列表
     *
     * @param status
     * @return
     */
    @GetMapping("/couponLists")
    public TableDataInfo couponList(Integer status) {
        startPage();
        List<LlCoupon> llCouponList = llCouponService.selectList(status);
        return getDataTable(llCouponList);
    }

    /**
     * 查询优惠券模块-套餐列表
     */
    @GetMapping("/thali/list")
    public TableDataInfo list(LlThali llThali) {
        startPage();
        List<LlThali> list = llThaliService.selectLlThaliList(llThali);
        return getDataTable(list);
    }

    /**
     * 获取优惠券模块-套餐详细信息
     */
    @GetMapping(value = "/thali/query/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(llThaliService.selectLlThaliById(id));
    }

    /**
     * 新增套餐（发布套餐）
     */
    @PostMapping("/grant/thali/")
    public AjaxResult add(@RequestBody LlThali llThali) {
        return llThaliService.insertLlThali(llThali);
    }

    /**
     * 修改优惠券模块-套餐
     */
    @PostMapping("/thali/edit")
    public AjaxResult edit(@RequestBody LlThali llThali) {
        return AjaxResult.success(llThaliService.updateLlThali(llThali));
    }

    /**
     * 删除优惠券模块-套餐
     */
    @DeleteMapping("/grant/delThail/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(llThaliService.deleteLlThaliByIds(ids));
    }

    /**
     * 导出优惠券模块-套餐列表
     */
    @PostMapping("/thali/export")
    public void export(HttpServletResponse response, LlThali llThali) {
        List<LlThali> list = llThaliService.selectLlThaliList(llThali);
        ExcelUtil<LlThali> util = new ExcelUtil<LlThali>(LlThali.class);
        util.exportExcel(response, list, "套餐数据");
    }
    /**
     * 发放优惠券
     *
     * @param couponGrantVo
     * @return
     */
    @PostMapping("/grant/coupon")
    public AjaxResult grantCoupon(@RequestBody CouponGrantVo couponGrantVo) throws ParseException {
        return llCouponService.grantCoupon(couponGrantVo);
    }
    /**
     * 优惠券列表展示
     */
    @GetMapping("/coupon/couponSelectList")
    public AjaxResult couponSelectList() {
        return llCouponService.couponSelectList();
    }
    /**
     * 车型列表展示
     */
    @GetMapping("/car/carSelectList")
    public AjaxResult carSelectList() {
        return llCarService.carSelectList();
    }
}
